<?php
class ModelLocalisationdistrict extends Model {
	public function adddistrict($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "district SET zone_id = '".(int)$data['zone_id']. "', status = '" . (int)$data['status'] . "', name = '" . $this->db->escape($data['name']) . "', code = '" . $this->db->escape($data['code']) . "', country_id = '" . (int)$data['country_id'] . "'");
			
		$this->cache->delete('district');
	}
	
	public function editdistrict($district_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "district SET zone_id = '".(int)$data['zone_id']. "' ,status = '" . (int)$data['status'] . "', name = '" . $this->db->escape($data['name']) . "', code = '" . $this->db->escape($data['code']) . "', country_id = '" . (int)$data['country_id'] . "' WHERE district_id = '" . (int)$district_id . "'");

		$this->cache->delete('district');
	}
	
	public function deletedistrict($district_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "district WHERE district_id = '" . (int)$district_id . "'");

		$this->cache->delete('district');	
	}
	
	public function getdistrict($district_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "district WHERE district_id = '" . (int)$district_id . "'");
		
		return $query->row;
	}
	
	public function getdistricts($data = array()) {
		$sql = "SELECT *, z.name, c.name AS country FROM " . DB_PREFIX . "district z LEFT JOIN " . DB_PREFIX . "country c ON (z.country_id = c.country_id)";
			//die($sql);
		$sort_data = array(
			'c.name',
			'z.name',
			'z.code'
		);	
			
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY c.name";	
		}
			
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}		
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
		
		$query = $this->db->query($sql);
		
		return $query->rows;
	}
	public function getdistrictsByZoneId($zone_id) {
		$district_data = $this->cache->get('district.' . (int)$zone_id);
	
		if (!$district_data) {
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "district WHERE zone_id = '" . (int)$zone_id . "' AND status = '1' ORDER BY name");
	
			$district_data = $query->rows;
			
			$this->cache->set('district.' . (int)$zone_id, $district_data);
		}
	
		return $district_data;
	}	
	public function getdistrictsByCountryId($country_id) {
		$district_data = $this->cache->get('district.' . (int)$country_id);
	
		if (!$district_data) {
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "district WHERE country_id = '" . (int)$country_id . "' ORDER BY name");
	
			$district_data = $query->rows;
			
			$this->cache->set('district.' . (int)$country_id, $district_data);
		}
	
		return $district_data;
	}
	
	public function getTotaldistricts() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "district");
		
		return $query->row['total'];
	}
				
	public function getTotaldistrictsByCountryId($country_id) {
		$query = $this->db->query("SELECT count(*) AS total FROM " . DB_PREFIX . "district WHERE country_id = '" . (int)$country_id . "'");
	
		return $query->row['total'];
	}
	public function get_zone(){
	$query = $this->db->query("SELECT $f1 FROM ".DB_PREFIX.$table." WHERE $f2='".$f2_value."'");
	if ($query) {
	$rs = $query->fetch_array($query);
	$f1_value = $rs[$f1];
	}
	return 'bvcb';
} 
}
?>